package com.cbs.ui.qrcodegen;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

import java.lang.ref.WeakReference;

/**
 * Created by broche on 6/22/16.
 */
public class QRCodeGenerator {
    /**
     * 创建黑色的二维码图片
     *
     * @param content
     * @param size     图片宽高，单位为px
     * @param delegate 创建二维码图片的代理
     */
    public static void encodeQRCode(String content, int size, QRCodeEncoder.Delegate delegate) {
        QRCodeEncoder.encodeQRCode(content, size, delegate);
    }

    public static void fillQRCode(final String content, final ImageView imageView) {
        new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int width = imageView.getWidth();
                int height = imageView.getHeight();
                int size = width < height ? width : height;
                QRCodeEncoder.encodeQRCode(content, size, new MyDelegate(imageView));
            }
        }.sendEmptyMessageDelayed(0, 300);
    }

    /**
     * 创建指定颜色的二维码图片
     *
     * @param content
     * @param size     图片宽高，单位为px
     * @param color    二维码图片的颜色
     * @param delegate 创建二维码图片的代理
     */
    public static void encodeQRCode(String content, int size, int color, QRCodeEncoder.Delegate delegate) {
        QRCodeEncoder.encodeQRCode(content, size, color, delegate);
    }

    public static void fillQRCode(final String content, final int color, final ImageView imageView) {
        new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int width = imageView.getWidth();
                int height = imageView.getHeight();
                int size = width < height ? width : height;
                QRCodeEncoder.encodeQRCode(content, size, color, new MyDelegate(imageView));
            }
        }.sendEmptyMessageDelayed(0, 300);

    }

    /**
     * 创建指定颜色的、带logo的二维码图片
     *
     * @param content
     * @param size     图片宽高，单位为px
     * @param color    二维码图片的颜色
     * @param logo     二维码图片的logo
     * @param delegate 创建二维码图片的代理
     */
    public static void encodeQRCode(String content, int size, int color, Bitmap logo, QRCodeEncoder.Delegate delegate) {
        QRCodeEncoder.encodeQRCode(content, size, color, logo, delegate);
    }

    public static void fillQRCode(final String content, final int color, final Bitmap logo, final ImageView imageView) {
        new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int width = imageView.getWidth();
                int height = imageView.getHeight();
                int size = width < height ? width : height;
                QRCodeEncoder.encodeQRCode(content, size, color, logo, new MyDelegate(imageView));
            }
        }.sendEmptyMessageDelayed(0, 300);
    }

    static class MyDelegate implements QRCodeEncoder.Delegate {
        private WeakReference<ImageView> imageViewWeakReference;

        public MyDelegate(ImageView imageView) {
            imageViewWeakReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        public void onEncodeQRCodeSuccess(Bitmap bitmap) {
            if (imageViewWeakReference.get() != null) {
                ImageView imageView = imageViewWeakReference.get();
                imageView.setImageBitmap(bitmap);
            }
        }

        @Override
        public void onEncodeQRCodeFailure() {
            // do nothing
        }
    }
}
